var express = require('express');
var router = express.Router();

//MD5数据加密
const { getMd5 } = require('../utils/crypto');

/* GET users listing. */
router.get('/', function (req, res, next) {
    res.send('respond with a resource');
});

//引入身份验证模块
const jwt = require('jsonwebtoken');

const { isAccess, register, login, isHave, registers, logins } = require("../service/userService");
//验证是否有该账号（出租方）
router.post('/isAccess', async function (req, res, next) {
    const { account } = req.body
    const data = await isAccess(account);
    res.send(data);
});

//注册账号(出租方)
router.post('/register', async function (req, res, next) {
    const { lessors } = req.body
    const { account, password, radio, phone } = lessors
    nwePassword = getMd5(password)
    const data = await register({ account, password: nwePassword, radio, phone });
    res.send(data);
});


//登录(出租方)
router.post('/login', async function (req, res, next) {
    const { account, password, radio } = req.body
    const newPassword = getMd5(password);
    const data = await login({ account, password: newPassword, radio });
    if (data.status) {
        const token = jwt.sign(
            { account },//保存用户信息
            'cxq',//密钥字符串
            { expiresIn: 60 * 2000 }//设置token保存的有效器单位是s
        )
        data.token = token;
        res.send(data);
    }
});

//判断是否登录
router.get('/isLogin', async function (req, res, next) {
    //获得到token
    const headersToken = req.get('Authorization');
    //得到的字符串以空格进行分割，并得到数组里面第二数据
    const token = headersToken.split(' ')[1];
    //解码 token 拿到需要的用户信息
    const { account } = jwt.verify(token, 'cxq');
    res.send({
        message: '身份验证通过',
        status: 1,
        data: account
    })
});


//验证租客的账号
router.post('/isHave', async function (req, res, next) {
    const { account } = req.body
    const data = await isHave(account);
    res.send(data);
});

//租客注册
router.post('/registers', async function (req, res, next) {
    const lessors = req.body
    const { account, password, name, Idcard, phone } = lessors
    nwePassword = getMd5(password)
    const data = await registers({ account, password: nwePassword, name, Idcard, phone });
    res.send(data);
});

//登录
router.post('/logins', async function (req, res, next) {
    const { account, password } = req.body
    const newPassword = getMd5(password);
    const data = await logins({ account, password: newPassword });
    if (data.status) {
        const token = jwt.sign(
            { account },//保存用户信息
            'cxq',//密钥字符串
            { expiresIn: 60 * 2000 }//设置token保存的有效器单位是s
        )
        data.token = token;
        res.send(data);
    }
});


//判断是否登录
router.get('/isLogins', async function (req, res, next) {
    //获得到token
    const headersToken = req.get('Authorization');
    //得到的字符串以空格进行分割，并得到数组里面第二数据
    const token = headersToken.split(' ')[1];
    //解码 token 拿到需要的用户信息
    const { account } = jwt.verify(token, 'cxq');
    res.send({
        message: '身份验证通过',
        status: 1,
        data: account
    })
});
module.exports = router;
